﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Index : BasePage
{
    private static string FModuleCode_Supplier = "003001";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    #region Ajex获取列表数据
    [WebMethod]
    public static AjexResponseModel GetGridDataHtml(int pageSize, int pageIndex, string OrderField, string QueryString)
    {
        AjexResponseModel response = new AjexResponseModel();

        #region 用户权限
        if (!CheckUserPermissionOperation2(FModuleCode_Supplier, "View"))
        {
            response.msg = "没有操作权限！请联系管理员";
            return response;
        }
        #endregion

        #region 组装查询条件
        string sWhere = " 1=1 ";
        sWhere += string.Format(" and CreateUserId={0} ", BasePage.CurrentSession2.UserID);
        sWhere += string.Format(" and NextRecordTime>'{0}' ", DateTime.Now.ToString("yyyy-MM-dd"));
        #endregion

        #region 排序
        if (OrderField.Trim().Length > 0)
        {
            OrderField = OrderField.Trim() + ",";
        }
        #endregion

        #region 获取数据
        DataTable dt = null;
        int pCount = 0;
        try
        {
            UtilityBLL ubll = new UtilityBLL();
            dt = ubll.GetListByPage("View_T_Customer", "*", OrderField + "NextRecordTime desc,CreateTime", sWhere, pageSize, pageIndex);

            #region 生成HTML
            StringBuilder sHtmlString = new StringBuilder();

            string sHtmlModel = @"
                <tr {0}>
                    <td>{1}</td>
                    <td>{2}</td>
                    <td>{3}</td>
                </tr>
            ";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sCustomerId = dt.Rows[i]["CustomerId"].ToString();

                sHtmlString.Append(string.Format(sHtmlModel
                    , (i % 2 == 0 ? "" : "class='ht-grid-even'")
                    , dt.Rows[i]["CustomerName"].ToString()
                    , dt.Rows[i]["NextRecordTime"].ToString()
                    , dt.Rows[i]["NextRecordContent"].ToString()
                    ));
            }
            #endregion

            response.gridHtml = sHtmlString.ToString();
            response.recordCound = pCount;
            response.pageIndex = pageIndex;
            response.pageSize = pageSize;
        }
        catch (Exception ex)
        {
            response.msg = ex.ToString();
        }
        #endregion

        return response;
    }


    [WebMethod]
    public static AjexResponseModel GetNodeDataHtml(int pageSize, int pageIndex, string OrderField, string QueryString, int Type)
    {
        AjexResponseModel response = new AjexResponseModel();

        #region 用户权限
        //if (!CheckUserPermissionOperation2(FModuleCode_Supplier, "View"))
        //{
        //    response.msg = "没有操作权限！请联系管理员";
        //    return response;
        //}
        #endregion

        #region 组装查询条件
        string sWhere = " Valid=1 ";
        sWhere += string.Format(" and CreateUserId={0} ", CurrentSession2.UserID);
        #endregion

        #region 排序
        //if (OrderField.Trim().Length > 0)
        //{
        //    OrderField = OrderField.Trim() + ",";
        //}
        #endregion

        #region 获取数据
        DataTable dt = null;
        int pCount = 0;
        try
        {
            UtilityBLL ubll = new UtilityBLL();
            dt = ubll.GetListByPage("T_Node", "*", OrderField + "OrderTime", sWhere, pageSize, pageIndex, 1, out pCount);

            #region 生成HTML
            StringBuilder sHtmlString = new StringBuilder();

            string sHtmlModel = "";
            if (Type == 1)
            {
                sHtmlModel = @"
                    <li class='ui-note-item'>
                        <div class='ui-note-body'>
                            <div class='ui-note-header'>
                                <i class='ui-note-icon'></i>
                                <span class='ui-note-time' title='创建时间'>{2}</span>
                                <div class='ui-note-tool'>{3}</div>
                            </div>
                            <div class='ui-note-content'>{1}</div>
                        </div>
                    </li>
                ";
            }
            else
            {
                sHtmlModel = @"
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                ";
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sNodeId = dt.Rows[i]["NodeId"].ToString();
                string sControl = "";
                sControl += string.Format("<a href=\"javascript:void(0)\" onclick=\"toFirst('{0}','{1}')\">前置</a>&nbsp;&nbsp;", "Index.aspx/SetNodeToFirst", sNodeId);
                sControl += string.Format("<a href=\"javascript:showWindow('我的便签<修改>','NodeEdit.aspx?Action={0}&NodeId={1}')\">修改</a>&nbsp;&nbsp;", "Modify", sNodeId);
                sControl += string.Format("<a href=\"javascript:void(0)\" onclick=\"delGridData('{0}','{1}')\">删除</a>", "Index.aspx/DelNodeGridData", sNodeId);
                sHtmlString.Append(string.Format(sHtmlModel
                    , (i % 2 == 0 ? "" : "class='ht-grid-even'")
                    , dt.Rows[i]["Content"].ToString()
                    , Convert.ToDateTime(dt.Rows[i]["CreateTime"]).ToString("yyyy-MM-dd")
                    , sControl
                ));
            }
            #endregion

            response.gridHtml = sHtmlString.ToString();
            response.recordCound = pCount;
            response.pageIndex = pageIndex;
            response.pageSize = pageSize;
        }
        catch (Exception ex)
        {
            response.msg = ex.ToString();
        }
        #endregion

        return response;
    }

    [WebMethod]
    public static string DelNodeGridData(string Id)
    {
        T_Node model = new T_Node();
        T_NodeBLL bll = new T_NodeBLL();
        string sMsg = "";

        try
        {
            model = bll.GetModel(Guid.Parse(Id));
            model.Valid = model.Valid == 1 ? 0 : 1;
            bll.Update(model);
        }
        catch (Exception ex)
        {
            sMsg = "操作失败！";
        }
        return sMsg;
    }

    [WebMethod]
    public static string SetNodeToFirst(string Id)
    {
        T_NodeBLL bll = new T_NodeBLL();
        T_Node model;
        string sMsg = "";
        try
        {
            model = bll.GetModel(Guid.Parse(Id));
            model.OrderTime = BasePage.GetNowTimeString();
            model.UpdateTime = BasePage.GetNowTimeString();
            model.UpdateUserId = BasePage.CurrentSession2.UserID;
            model.OrderTime = BasePage.GetNowTimeString();
            bll.Update(model);
        }
        catch (Exception ex)
        {
            sMsg = "操作失败！";
        }
        return sMsg;
    }
    #endregion
}